package example1;

import java.util.*;

/**
 * <h3>designPattern</h3>
 * <p>Set中迭代器的使用</p>
 * @author : zhang.bw
 * @date : 2021-02-27 16:20
 **/
public class SetIterator1 {

	public static void main(String[] args) {

		List<Student> list = listStudent();
		System.out.println("使用list进行遍历");
		listASC(list);
		listDESC(list);
		setASC();
		setDESC();
	}

	//将数组存储在set中
	public static void setASC()
	{
		System.out.println("使用set进行顺序输出");
		Set<Student> set = new TreeSet<Student>(new setCompare()) ;
		set.add(new Student(3, "李四", 19));
		set.add(new Student(2, "张三", 18));
		set.add(new Student(5, "李六", 19));
		set.add(new Student(1, "王五", 18));
		set.add(new Student(6, "赵六", 19));

		for(Student stu: set) {
			System.out.println("学号：" + stu.getId() + "  姓名：" + stu.getName() + "  年龄：" + stu.getAge());
		}

	}

	//将数组存储在set中
	public static void setDESC()
	{
		System.out.println("使用set进行逆序输出");
		Set<Student> set = new TreeSet<Student>(new setCompareDESC()) ;
		set.add(new Student(3, "李四", 19));
		set.add(new Student(2, "张三", 18));
		set.add(new Student(5, "李六", 19));
		set.add(new Student(1, "王五", 18));
		set.add(new Student(6, "赵六", 19));

		for(Student stu: set) {
			System.out.println("学号：" + stu.getId() + "  姓名：" + stu.getName() + "  年龄：" + stu.getAge());
		}

	}

	//将数组存储在List容器中
	public static List<Student> listStudent() {
		List<Student> list = new ArrayList<Student>();
		list.add(new Student(3, "李四", 19));
		list.add(new Student(2, "张三", 18));
		list.add(new Student(5, "李六", 19));
		list.add(new Student(1, "王五", 18));
		list.add(new Student(6, "赵六", 19));
		return list;
	}

	public static void listASC(List<Student> list) {
		System.out.println("按照学号顺序输出");
		Collections.sort(list,new Comparator<Student>() {

			@Override
			public int compare(Student o1, Student o2) {
				// TODO 自动生成的方法存根
				if (o1.getId() > o2.getId()) {
					return 1;
				}
				if (o1.getId() == o2.getId()) {
					return 0;
				}
				return -1;
			}

		});

		for(Student stu: list) {
			System.out.println("学号：" + stu.getId() + "  姓名：" + stu.getName() + "  年龄：" + stu.getAge());
		}

	}

	public static void listDESC(List<Student> list) {
		System.out.println("按照学号逆序输出");
		Collections.sort(list,new Comparator<Student>() {

			@Override
			public int compare(Student o1, Student o2) {
				// TODO 自动生成的方法存根
				if (o1.getId() < o2.getId()) {
					return 1;
				}
				if (o1.getId() == o2.getId()) {
					return 0;
				}
				return -1;
			}
		});

		for(Student stu: list) {
			System.out.println("学号：" + stu.getId() + "  姓名：" + stu.getName() + "  年龄：" + stu.getAge());
		}
	}

}
